<template>
  <Dialog
    v-model:visible="dialog.visible"
    modal
    :header="dialogTitle"
    @show="openHandler"
    @hide="closeHandler"
  >
    <el-form
      ref="dataForm"
      :model="entity"
      :disabled="operation == 3"
      label-width="auto"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item
            prop="parentId"
            label="父菜单"
            :rules="{
              required: true,
              message: '请选择父菜单',
              trigger: 'blur',
            }"
          >
            <TreeSelect
              v-model="entity.parentId"
              :options="menuOptions"
              selectionMode="single"
              placeholder="请选择父菜单"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item
            prop="menuName"
            label="名称"
            :rules="{ required: true, message: '请输入名称', trigger: 'blur' }"
          >
            <InputText v-model="entity.menuName" placeholder="请输入名称" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item
            prop="menuType"
            label="菜单类型"
            :rules="{
              required: true,
              message: '请选择菜单类型',
              trigger: 'blur',
            }"
          >
            <SelectButton
              v-model="entity.menuType"
              :options="menuType"
              optionValue="value"
              optionLabel="label"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item
            prop="menuPermission"
            label="权限"
            :rules="{
              required: entity.menuType != 'CATALOG',
              message: '请输入展示权限',
              trigger: 'blur',
            }"
          >
            <InputText
              v-model="entity.menuPermission"
              placeholder="请输入权限"
            />
          </el-form-item>
        </el-col>

        <el-col v-if="entity.menuType == 'MENU'" :span="12">
          <el-form-item
            prop="menuPage"
            label="展示页面"
            :rules="{
              required: true,
              message: '请输入展示页面',
              trigger: 'blur',
            }"
          >
            <InputText v-model="entity.menuPage" placeholder="请输入展示页面" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col
          v-if="entity.menuType == 'MENU' || entity.menuType == 'CATALOG'"
          :span="12"
        >
          <el-form-item prop="menuIcon" label="图标">
            <InputText v-model="entity.menuIcon" placeholder="请输入图标" />
          </el-form-item>
        </el-col>

        <el-col v-if="entity.menuType != 'AUTHORITY'" :span="12">
          <el-form-item prop="menuOrder" label="排序">
            <InputText v-model="entity.menuOrder" placeholder="请输入排序" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <cancel-button @click="dialog.visible = false" />
      <save-button
        v-if="operation != 3"
        :loading="loading"
        @click="saveHandler"
      />
    </template>
  </Dialog>
</template>
<script>
import { save, update } from '@/api/upms/menu'
import { menuType } from '@/components/dictionary/index.js'

export default {
  name: 'MenuEdit',
  props: {
    dialog: {
      type: Object,
      required: true,
    },
  },
  emits: ['success'],
  data() {
    return {
      loading: false,
      entity: {},
      titleMap: {
        1: '新建菜单表',
        2: '编辑菜单表',
        3: '查看菜单表',
      },
      menuOptions: [],
    }
  },
  computed: {
    operation() {
      return this.dialog.operation
    },
    dialogTitle() {
      return this.titleMap[this.dialog.operation]
    },
    menuType() {
      return menuType
    },
  },
  methods: {
    openHandler() {
      this.entity = Object.assign(
        {
          parentId: null,
          menuButton: false,
          menuType: 'MENU',
        },
        this.dialog.entity
      )

      this.menuOptions = [...this.dialog.menus]
      this.menuOptions.unshift({
        key: 0,
        label: '无',
        data: {
          id: 0,
          menuName: '无',
        },
      })
    },

    closeHandler() {
      this.entity = {}
    },
    saveHandler() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.loading = true
          let edit = this.operation == 1 ? save : update
          edit(this.entity)
            .then(() => {
              this.dialog.visible = false
              this.$message.success('保存成功')
              this.$emit('success')
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    },
  },
}
</script>
